<?php

namespace app\api\controller\kuyou\applet;
use think\Db;
/**
 * 资金接口
 */
class Capital extends Common
{
    protected $noNeedRight = '*';

    /**
     * 核销单选择预收单据
     * @ApiMethod (POST)
     */
    public function choseList(){
        if ($this->request->isPost()) {
            $customers_id = $this->request->post('customers_id');
            if(empty($customers_id)){
                $this->error('请选择客户','',500);
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;

            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            $where = [];
            $where['customers_id'] = $customers_id;
            $where['purchase_type'] = ['in',[1,6]];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['purchase_code'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }

            if($pageStatus){
                $list = Db::name('kuyou_writeoff_source')
                    ->where($where)
                    ->where(function ($query){
                        $query->where('id', 'NOT IN', function ($query) {
                            $query->name('kuyou_writeoff_log')->field('source_id');
                        })
                            ->whereOr('id', 'IN', function ($query) {
                                $query->name('kuyou_writeoff_log')->where(['write_off'=>['<>','0']])->field('source_id');
                            });
                    })
                    ->order(['purchase_type'=>'DESC','purchase_time'=>'ASC'])
                    ->page($page,$limit)
                    ->select();
            }else{
                $list = Db::name('kuyou_writeoff_source')
                    ->where($where)
                    ->where(function ($query){
                        $query->where('id', 'NOT IN', function ($query) {
                            $query->name('kuyou_writeoff_log')->field('source_id');
                        })
                            ->whereOr('id', 'IN', function ($query) {
                                $query->name('kuyou_writeoff_log')->where(['write_off'=>['<>','0']])->field('source_id');
                            });
                    })
                    ->order(['purchase_type'=>'DESC','purchase_time'=>'ASC'])
                    ->select();
            }
            $rowcount = Db::name('kuyou_writeoff_source')
                ->where($where)
                ->where(function ($query){
                    $query->where('id', 'NOT IN', function ($query) {
                        $query->name('kuyou_writeoff_log')->field('source_id');
                    })
                        ->whereOr('id', 'IN', function ($query) {
                            $query->name('kuyou_writeoff_log')->where(['write_off'=>['<>','0']])->field('source_id');
                        });
                })
                ->count();

            foreach ($list as $list_k=>$list_v){
                $list[$list_k]['money_tax_price'] = $list_v['price'];
                $list[$list_k]['pay_money'] = $list_v['money'];
                $list[$list_k]['money'] = $list_v['write_off'];
                $list[$list_k]['purchase_type_id'] = $list_v['id'];
                $list[$list_k]['purchase_codes'] = $list_v['purchase_code'];
                if($list_v['purchase_type']>4){
                    $list[$list_k]['purchase_type'] = '期初余额';
                    $list[$list_k]['purchase_code'] = '期初余额';
                }else{
                    $list[$list_k]['purchase_type'] = '收款';
                }
                $list[$list_k]['purchase_time'] = date('Y-m-d',$list_v['purchase_time']);
                $list[$list_k]['remark'] = $list_v['remark'];
                $list[$list_k]['type'] = $list_v['type'];

                $writeoff_log = Db::name('kuyou_writeoff_log')->where(['source_id'=>$list_v['id']])->field('money,write_off')->find();
                if($writeoff_log){
                    $list[$list_k]['money'] = $writeoff_log['write_off'];
                    $list[$list_k]['pay_money'] = $writeoff_log['money'];
                }else{
                    $list[$list_k]['pay_money'] = 0;
                }
                unset($list[$list_k]['admin_id']);
            }

            $result = ['list'=>$list,'count'=>$rowcount];
            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }

        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 核销单选择预付单据
     * @ApiMethod (POST)
     */
    public function choseAdvanceList(){
        if ($this->request->isPost()) {
            $suppliers_id = $this->request->post('suppliers_id');
            if(empty($suppliers_id)){
                $this->error('请选择供应商','',500);
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;

            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            $suppliers = Db::name('kuyou_suppliers')->where(['id'=>$suppliers_id])->find();
            $where = [];
            $where['suppliers_code'] = $suppliers['supplyer_code'];
            $where['purchase_type'] = ['in',[2,6]];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['purchase_code'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }

            if($pageStatus){
                $list = Db::name('kuyou_writeoff_source')
                    ->where($where)
                    ->where(function ($query){
                        $query->where('id', 'NOT IN', function ($query) {
                            $query->name('kuyou_writeoff_log')->field('source_id');
                        })
                            ->whereOr('id', 'IN', function ($query) {
                                $query->name('kuyou_writeoff_log')->where(['write_off'=>['<>','0']])->field('source_id');
                            });
                    })
                    ->order(['purchase_type'=>'DESC','purchase_time'=>'ASC'])
                    ->page($page,$limit)
                    ->select();
            }else{
                $list = Db::name('kuyou_writeoff_source')
                    ->where($where)
                    ->where(function ($query){
                        $query->where('id', 'NOT IN', function ($query) {
                            $query->name('kuyou_writeoff_log')->field('source_id');
                        })
                            ->whereOr('id', 'IN', function ($query) {
                                $query->name('kuyou_writeoff_log')->where(['write_off'=>['<>','0']])->field('source_id');
                            });
                    })
                    ->order(['purchase_type'=>'DESC','purchase_time'=>'ASC'])
                    ->select();
            }
            $rowcount = Db::name('kuyou_writeoff_source')
                ->where($where)
                ->where(function ($query){
                    $query->where('id', 'NOT IN', function ($query) {
                        $query->name('kuyou_writeoff_log')->field('source_id');
                    })
                        ->whereOr('id', 'IN', function ($query) {
                            $query->name('kuyou_writeoff_log')->where(['write_off'=>['<>','0']])->field('source_id');
                        });
                })
                ->count();

            foreach ($list as $list_k=>$list_v){
                $list[$list_k]['money_tax_price'] = $list_v['price'];
                $list[$list_k]['pay_money'] = $list_v['money'];
                $list[$list_k]['money'] = $list_v['write_off'];
                $list[$list_k]['purchase_type_id'] = $list_v['id'];
                $list[$list_k]['purchase_codes'] = $list_v['purchase_code'];
                if($list_v['purchase_type']>4){
                    $list[$list_k]['purchase_type'] = '期初余额';
                    $list[$list_k]['purchase_code'] = '期初余额';
                }else{
                    $list[$list_k]['purchase_type'] = '付款';
                }
                $list[$list_k]['purchase_time'] = date('Y-m-d',$list_v['purchase_time']);
                $list[$list_k]['remark'] = $list_v['remark'];
                $list[$list_k]['type'] = $list_v['type'];

                $writeoff_log = Db::name('kuyou_writeoff_log')->where(['source_id'=>$list_v['id']])->field('money,write_off')->find();
                if($writeoff_log){
                    $list[$list_k]['money'] = $writeoff_log['write_off'];
                    $list[$list_k]['pay_money'] = $writeoff_log['money'];
                }else{
                    $list[$list_k]['pay_money'] = 0;
                }
                unset($list[$list_k]['admin_id']);
            }

            $result = ['list'=>$list,'count'=>$rowcount];
            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 核销单选择应收单据
     * @ApiMethod (POST)
     */
    public function choseReceivable(){
        if ($this->request->isPost()) {
            $customers_id = $this->request->post('customers_id');
            if(empty($customers_id)){
                $this->error('请选择客户','',500);
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;

            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            $where = [];
            $where['customers_id'] = $customers_id;
            $where['purchase_type'] = ['in',[3,5]];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['purchase_code'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }

            if($pageStatus){
                $list = Db::name('kuyou_writeoff_source')
                    ->where($where)
                    ->where(function ($query){
                        $query->where('id', 'NOT IN', function ($query) {
                            $query->name('kuyou_writeoff_log')->field('source_id');
                        })
                            ->whereOr('id', 'IN', function ($query) {
                                $query->name('kuyou_writeoff_log')->where(['write_off'=>['<>','0']])->field('source_id');
                            });
                    })
                    ->order(['purchase_type'=>'DESC','purchase_time'=>'ASC'])
                    ->page($page,$limit)
                    ->select();
            }else{
                $list = Db::name('kuyou_writeoff_source')
                    ->where($where)
                    ->where(function ($query){
                        $query->where('id', 'NOT IN', function ($query) {
                            $query->name('kuyou_writeoff_log')->field('source_id');
                        })
                            ->whereOr('id', 'IN', function ($query) {
                                $query->name('kuyou_writeoff_log')->where(['write_off'=>['<>','0']])->field('source_id');
                            });
                    })
                    ->order(['purchase_type'=>'DESC','purchase_time'=>'ASC'])
                    ->select();
            }
            $rowcount = Db::name('kuyou_writeoff_source')
                ->where($where)
                ->where(function ($query){
                    $query->where('id', 'NOT IN', function ($query) {
                        $query->name('kuyou_writeoff_log')->field('source_id');
                    })
                        ->whereOr('id', 'IN', function ($query) {
                            $query->name('kuyou_writeoff_log')->where(['write_off'=>['<>','0']])->field('source_id');
                        });
                })
                ->count();

            foreach ($list as $list_k=>$list_v){
                $list[$list_k]['money_tax_price'] = $list_v['price'];
                $list[$list_k]['pay_money'] = $list_v['money'];
                $list[$list_k]['money'] = $list_v['write_off'];
                $list[$list_k]['purchase_type_id'] = $list_v['id'];
                $list[$list_k]['purchase_codes'] = $list_v['purchase_code'];
                if($list_v['purchase_type']>4){
                    $list[$list_k]['purchase_type'] = '期初余额';
                    $list[$list_k]['purchase_code'] = '期初余额';
                }else{
                    if($list_v['type']==1){
                        $list[$list_k]['purchase_type'] = '普通销售';
                    }else if($list_v['type']==4){
                        $list[$list_k]['purchase_type'] = '核销';
                    }else{
                        $list[$list_k]['purchase_type'] = '销售退货';
                    }
                }
                $list[$list_k]['purchase_time'] = date('Y-m-d',$list_v['purchase_time']);
                $list[$list_k]['remark'] = $list_v['remark'];
                $list[$list_k]['type'] = $list_v['type'];

                $writeoff_log = Db::name('kuyou_writeoff_log')->where(['source_id'=>$list_v['id']])->field('money,write_off')->find();
                if($writeoff_log){
                    $list[$list_k]['money'] = $writeoff_log['write_off'];
                    $list[$list_k]['pay_money'] = $writeoff_log['money'];
                }else{
                    $list[$list_k]['pay_money'] = 0;
                }
                unset($list[$list_k]['admin_id']);
            }
            $result = ['list'=>$list,'count'=>$rowcount];
            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 核销单选择应付单据
     * @ApiMethod (POST)
     */
    public function choseCopeWith(){
        if ($this->request->isPost()) {
            $suppliers_id = $this->request->post('suppliers_id');
            if(empty($suppliers_id)){
                $this->error('请选择供应商','',500);
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;

            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            $suppliers = Db::name('kuyou_suppliers')->where(['id'=>$suppliers_id])->find();
            $where = [];
            $where['suppliers_code'] = $suppliers['supplyer_code'];
            $where['purchase_type'] = ['in',[4,5]];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['purchase_code'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }

            if($pageStatus){
                $list = Db::name('kuyou_writeoff_source')
                    ->where($where)
                    ->where(function ($query){
                        $query->where('id', 'NOT IN', function ($query) {
                            $query->name('kuyou_writeoff_log')->field('source_id');
                        })
                            ->whereOr('id', 'IN', function ($query) {
                                $query->name('kuyou_writeoff_log')->where(['write_off'=>['<>','0']])->field('source_id');
                            });
                    })
                    ->order(['purchase_type'=>'DESC','purchase_time'=>'ASC'])
                    ->page($page,$limit)
                    ->select();
            }else{
                $list = Db::name('kuyou_writeoff_source')
                    ->where($where)
                    ->where(function ($query){
                        $query->where('id', 'NOT IN', function ($query) {
                            $query->name('kuyou_writeoff_log')->field('source_id');
                        })
                            ->whereOr('id', 'IN', function ($query) {
                                $query->name('kuyou_writeoff_log')->where(['write_off'=>['<>','0']])->field('source_id');
                            });
                    })
                    ->order(['purchase_type'=>'DESC','purchase_time'=>'ASC'])
                    ->select();
            }

            $rowcount = Db::name('kuyou_writeoff_source')
                ->where($where)
                ->where(function ($query){
                    $query->where('id', 'NOT IN', function ($query) {
                        $query->name('kuyou_writeoff_log')->field('source_id');
                    })
                        ->whereOr('id', 'IN', function ($query) {
                            $query->name('kuyou_writeoff_log')->where(['write_off'=>['<>','0']])->field('source_id');
                        });
                })
                ->count();

            foreach ($list as $list_k=>$list_v){
                $list[$list_k]['money_tax_price'] = $list_v['price'];
                $list[$list_k]['pay_money'] = $list_v['money'];
                $list[$list_k]['money'] = $list_v['write_off'];
                $list[$list_k]['purchase_type_id'] = $list_v['id'];
                $list[$list_k]['purchase_codes'] = $list_v['purchase_code'];
                if($list_v['purchase_type']>4){
                    $list[$list_k]['purchase_type'] = '期初余额';
                    $list[$list_k]['purchase_code'] = '期初余额';
                }else{
                    if($list_v['type']==1){
                        $list[$list_k]['purchase_type'] = '普通购货';
                    }else{
                        $list[$list_k]['purchase_type'] = '购货退货';
                    }
                }
                $list[$list_k]['purchase_time'] = date('Y-m-d',$list_v['purchase_time']);
                $list[$list_k]['remark'] = $list_v['remark'];
                $list[$list_k]['type'] = $list_v['type'];

                $writeoff_log = Db::name('kuyou_writeoff_log')->where(['source_id'=>$list_v['id']])->field('money,write_off')->find();
                if($writeoff_log){
                    $list[$list_k]['money'] = $writeoff_log['write_off'];
                    $list[$list_k]['pay_money'] = $writeoff_log['money'];
                }else{
                    $list[$list_k]['pay_money'] = 0;
                }
                unset($list[$list_k]['admin_id']);
            }
            $result = ['list'=>$list,'count'=>$rowcount];
            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

}
